/**
 * 文件操作
 */
var o = {};

var myfile = new Function();
myfile.prototype = o;
/**
 * 未通过全选
 */
o.checkUNAPPROVE = function(obj) {
	if (obj.attr('checked') == true) {
		$('#UNAPPROVE .selectItem').attr('checked', 'checked');
	} else {
		$('#UNAPPROVE .selectItem').attr('checked', '');
	}
}
/**
 * 已通过全选
 */
o.checkAPPROVE = function(obj) {
	if (obj.attr('checked') == true) {
		$('#APPROVE .selectItem').attr('checked', 'checked');
	} else {
		$('#APPROVE .selectItem').attr('checked', '');
	}
}
/**
 * 提交中全选
 */
o.checkSUBMITTING = function(obj) {
	if (obj.attr('checked') == true) {
		$('#SUBMITTING .selectItem').attr('checked', 'checked');
	} else {
		$('#SUBMITTING .selectItem').attr('checked', '');
	}
}
/**
 * 删除未通过
 */
o.delUnApprove = function() {
	var ids = "";
	$('#UNAPPROVE .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要删除的记录!");
		return;
	}
	// return ids;
	$.ajax( {
		type : "POST",
		url : "myfile-delUnApprove.action",
		data : "ids="+ids,
		success : function(msg) {
			alert(msg.jsonResult);
			window.location.reload(false);
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    // 通常 textStatus 和 errorThrown 之中
		    // 只有一个会包含信息
		    alert("出错了："+errorThrown);
		}
	});
}
/**
 * 删除已通过
 */
o.delApprove = function() {
	var ids = "";
	$('#APPROVE .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要删除的记录!");
		return;
	}
	// return ids;
	$.ajax( {
		type : "POST",
		url : "myfile-delApprove.action",
		data : "ids="+ids,
		success : function(msg) {
			//$.ligerDialog.success(msg.jsonResult);
			alert(msg.jsonResult);
			window.location.reload(false);
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    // 通常 textStatus 和 errorThrown 之中
		    // 只有一个会包含信息
		    alert("出错了："+errorThrown);
		}
	});
}
/**
 * 删除提交中
 */
o.delSubmitting = function() {
	var ids = "";
	$('#SUBMITTING .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要删除的记录!");
		return;
	}
	// return ids;
	$.ligerDialog.confirm('确定删除?', function(yes)
			{
				if(yes){
					$.ajax( {
						type : "POST",
						url : "myfile-delSubmitting.action",
						data : "ids="+ids,
						success : function(msg) {
							//$.ligerDialog.success(msg.jsonResult);
							alert(msg.jsonResult);
							window.location.reload(false);
						},
						error:function (XMLHttpRequest, textStatus, errorThrown) {
						    // 通常 textStatus 和 errorThrown 之中
						    // 只有一个会包含信息
						    alert("出错了："+errorThrown);
						}
					});
				}
			});
}
/**
 * 我的收藏全选
 */
o.checkFAVORITE = function(obj) {
	if (obj.attr('checked') == true) {
		$('#FAVORITE .selectItem').attr('checked', 'checked');
	} else {
		$('#FAVORITE .selectItem').attr('checked', '');
	}
}
/**
 * 删除我的收藏
 */
o.delFavorite = function() {
	var ids = "";
	$('#FAVORITE .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要删除的记录!");
		return;
	}
	$.ajax( {
		type : "POST",
		url : "myfile-delFavorite.action",
		data : "ids="+ids,
		success : function(msg) {
			//$.ligerDialog.success(msg.jsonResult);
			alert(msg.jsonResult);
			window.location.reload(false);
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    // 通常 textStatus 和 errorThrown 之中
		    $.ligerDialog.error("出错了："+errorThrown);
		}
	});
}
/**
 * 选择分类
 */
o.chooseCate=function(){
	$('<div id="choose_folder"></div>').appendTo($('body'));;
	$("#choose_folder").load(ROOT+'/category/category-show.action',function(){
		$.ligerDialog.open({target:$("#choose_folder") , height: 300, width: 300,modal:true,buttons: [
		                                                                                               { text: '确定', onclick: function (item, dialog) {
		                                                                                               			var manager = $("#tree1").ligerGetTreeManager();
		                                                                                               		 	var note = manager.getSelected(); 
		                                                                                       					$('#cateinput').text(note.data.text);
		                                                                                       					$('#cateinputHidden').val(note.data.text);
		                                                                                       					$('#cateIdHidden').val(note.data.id);
		                                                                                       					dialog.close(); 
		                                                                                               		 	
		                                                                                               	} },
		                                                                                               { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
		                                                                                            ]
		                                                                                            });
	});
	
}
 
o.chooseCateSingle = function(id) {
	$('<div id="choose_folder"></div>').appendTo($('body'));
	;
	$("#choose_folder").load(ROOT + '/category/category-show.action',
			function() {
				$.ligerDialog.open( {
					target : $("#choose_folder"),
					height : 300,
					width : 300,
					modal : true,
					buttons : [ {
						text : '确定',
						onclick : function(item, dialog) {
							var manager = $("#tree1").ligerGetTreeManager();
							var note = manager.getSelected();
							$('#'+id+'cateinput').text(note.data.text);
							$('#'+id+'cateinputHidden').val(note.data.text);
							$('#'+id+'cateIdHidden').val(note.data.id);
							dialog.close();

						}
					}, {
						text : '取消',
						onclick : function(item, dialog) {
							dialog.close();
						}
					} ]
				});
			});

}
/**
* 更改分类
*/
o.changeCate=function(fileId){
	$('<div id="choose_folder"></div>').appendTo($('body'));;
	$("#choose_folder").load(ROOT+'/category/category-show.action');
	$.ligerDialog.open({target:$("#choose_folder") , height: 300, width: null,modal:true,buttons: [
       { text: '确定', onclick: function (item, dialog) {
       			var manager = $("#tree1").ligerGetTreeManager();
       		 	var note = manager.getSelected(); 
					$.ajax( {
						type : "POST",
						url : "myfile-changeCate.action",
						data : "ids="+fileId+"&category="+note.data.text,
						success : function(msg) {
							//$.ligerDialog.success(msg.jsonResult);
							alert(msg.jsonResult);
							window.location.reload(false);
						},
						error:function (XMLHttpRequest, textStatus, errorThrown) {
						    // 通常 textStatus 和 errorThrown 之中
						    $.ligerDialog.error("出错了："+errorThrown);
						}
					});
					dialog.close(); 
       		 	
       	} },
       { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
    ]
    });
}
o.moveFile = function(fileId) {
	$.ajax( {
		type : "POST",
		url : "myfile-moveFile.action",
		data : "fileId=" + fileId,
		success : function(msg) {
		alert(msg.jsonResult);
		window.location.reload(false);
	},
	error : function(XMLHttpRequest, textStatus, errorThrown) {
		$.ligerDialog.error("出错了：" + errorThrown);
	}
	});
}
o.changeCateBatch=function(){
	var ids = "";
	$('#SUBMITTING .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要更改的记录!");
		return;
	}
	o.changeCate(ids);
}
/**
* 通过
*/
o.approve=function(fileId){
	$.ajax( {
		type : "POST",
		url : "myfile-approve.action",
		data : "ids="+fileId,
		success : function(msg) {
			//$.ligerDialog.success(msg.jsonResult);
			alert(msg.jsonResult);
			window.location.reload(false);
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    // 通常 textStatus 和 errorThrown 之中
		    $.ligerDialog.error("出错了："+errorThrown);
		}
	});
}
o.approveBatch=function(){
	var ids = "";
	$('#SUBMITTING .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择通过的记录!");
		return;
	}
	o.approve(ids);
}
/**
* 不通过
*/
o.unApprove=function(fileId){
	$.ajax( {
		type : "POST",
		url : "myfile-unApprove.action",
		data : "ids="+fileId,
		success : function(msg) {
			//$.ligerDialog.success(msg.jsonResult);
			alert(msg.jsonResult);
			window.location.reload(false);
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    // 通常 textStatus 和 errorThrown 之中
		    $.ligerDialog.error("出错了："+errorThrown);
		}
	});
}
o.unApproveBatch=function(){
	var ids = "";
	$('#SUBMITTING .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择不通过的记录!");
		return;
	}
	o.unApprove(ids);
}
/**
 * 上传文件对话框
 */
o.uploadSingleFile=function(tempFileId){
	$.ligerDialog.open({url:ROOT+'/myfile/myfile-gotoSingleUploadStep2.action?tempFileId='+tempFileId, height: 400, width: 600,modal:true,buttons: [
        { text: '关闭', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
}
 /**
  * 上传文件对话框
  */
 o.uploadImage=function(tempFileId){
 	$.ligerDialog.open({url:ROOT+'/myfile/myfile-gotoImageUploadStep2.action?tempFileId='+tempFileId, height: 400, width: 600,modal:true,buttons: [
         { text: '关闭', onclick: function (item, dialog) { dialog.close(); } }
      ]
      });
 }
  o.uploadIcon=function(tempFileId){
	 	$.ligerDialog.open({url:ROOT+'/myfile/myfile-gotoIconUploadStep2.action?tempFileId='+tempFileId, height: 400, width: 600,modal:true,buttons: [
	         { text: '关闭', onclick: function (item, dialog) { dialog.close(); } }
	      ]
	      });
	 }
/**
  * 上传文件对话框
  */
 o.uploadAdminFile=function(tempFileId){
 	$.ligerDialog.open({url:ROOT+'/myfile/myfile-gotoAdminUploadStep2.action?tempFileId='+tempFileId, height: 400, width: 600,modal:true,buttons: [
         { text: '关闭', onclick: function (item, dialog) { dialog.close(); } }
      ]
      });
 }
o.uploadNewsFile=function(tempFileId){
	 	$.ligerDialog.open({url:ROOT+'/myfile/myfile-gotoNewsUploadStep2.action?tempFileId='+tempFileId, height: 400, width: 600,modal:true,buttons: [
	         { text: '关闭', onclick: function (item, dialog) { dialog.close(); } }
	      ]
	      });
}
/**
 * 编辑文件提交
 */
o.editFile=function(){
	var form=$('#saveform');
	var legal =form.validate();
	if(legal){
		$.ajax( {
			type : "POST",
			url : ROOT+'/myfile/myfile-editFile.action', 
			data : form.serialize(), 
			success : function(msg) {
				//$.ligerDialog.success(msg.jsonResult);
				alert(msg.jsonResult);
				window.location=$('#referrer').val();
			},
			error:function (XMLHttpRequest, textStatus, errorThrown) {
			    $.ligerDialog.error("出错了："+errorThrown);

			}
		});
	}
	return;
}
/**
 * 添加我的收藏
 */
o.addFav=function(fileId){
	$.ajax( {
		type : "POST",
		url : ROOT+'/myfile/myfile-addFavorite.action', 
		data : 'fileId='+fileId, 
		success : function(msg) {
			$.ligerDialog.success(msg.jsonResult);
			$('#addFavorite').text('已收藏');
			$('#addFavorite').attr("href","");
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    $.ligerDialog.error("出错了："+errorThrown);

		}
	});
}

o.batchDown=function(){
	var ids = "";
	$('#SUBMITTING .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要打包下载的记录!");
		return;
	}
	window.location="download.action?ids="+ids+"&from=1&folder=temp";
	
}
o.checkFile=function(name){
	var ise=false;
	$.ajax( {
		type : "POST",
		async:false,
		url : ROOT+'/myfile/myfile-checkFile.action', 
		data : 'fileName='+name, 
		success : function(msg) {
			if(msg.jsonCode==0){
				alert(msg.jsonResult);
				ise=true;
			}
		}
	});
	return ise;
}